package br.edu.ufcg.greengrid.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import br.edu.ufcg.greengrid.model.job.Job;
import br.edu.ufcg.greengrid.model.job.JobWithCheckpoint;

public class JobWithCheckpointTest {

	@Test
	public void testClearJob() {
		long runtime = 10;
		JobWithCheckpoint j = new JobWithCheckpoint(runtime);
		for (int clock = 0; clock < runtime / 2; clock++) {
			j.update();
			j.incMakespan();
			assertFalse(j.done());
		}

		j.clear();

		assertJobIsRun(j, runtime / 2);
		assertEquals(runtime, j.getMakespan());
	}

	// Auxiliary methods
	private void assertJobIsRun(Job j, long runtime) {
		long clock;
		
		assertTrue(!j.done());
		for (clock = 0; clock < runtime - 1; clock++) {
			if(!j.done()){
				j.incMakespan();
			}
			j.update();
			assertFalse(String.format("i == %d", clock), j.done());
		}

		if(!j.done()){
			j.incMakespan();
		}
		j.update();
		assertTrue(String.format("i == %d", clock), j.done());
	}

}
